/*
 * 文件名：PropertyConfiguration.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：qianfm
 * 修改时间：Sep 24, 2012
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.common.manager;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


/**
 * 〈系统配置文件加载〉
 * 〈功能详细描述〉
 * @author qianfm
 * @version Sep 24, 2012
 * @see PropertyConfiguration
 * @since
 */

public class PropertyConfiguration extends PropertyPlaceholderConfigurer
{
    private static final String PREFIX_ENCRYPT = "{enc}";
    
    private List<String> configs;
    
    private String serverRoot;
    
    public void init() throws Exception
    {
        if (StringUtils.isEmpty(serverRoot))
        {
            serverRoot = GeneralUtils.toLocalFilePathWithSeperatorEnd(System.getProperty(Constants.SERVER_ROOT));
        }
        int len = this.configs.size();
        // len个文件型 + 1个数据库型
        java.util.Properties[] props = new java.util.Properties[len + 1];
        for (int i = 0; i < len; i++)
        {
            props[i] = parseEncValues(loadFromFile(new File(serverRoot
                    + configs.get(i))));
        }
        
        this.setPropertiesArray(props);
    }
    
    private Properties loadFromFile(File file) throws IOException
    {
        Properties prop = new Properties();
        
        InputStream inputStream = FileUtils.openInputStream(file);
        if (inputStream != null)
        {
            prop.load(inputStream);
        }
        else
        {
            prop = null;
        }
        return prop;
    }
    
    /**
     * 
     * Description: 针对加密的处理，比如数据库密码等<br>
     * Implement: <br>
     * @param prop
     * @return
     * @throws Exception
     * @see
     * @return Properties
     * @author qianfm
     */
    private Properties parseEncValues(Properties prop) throws Exception
    {
        List<String> keys = propertyNames(prop);
        for (String key : keys)
        {
            String value = prop.getProperty(key);
            if (value.startsWith(PREFIX_ENCRYPT))
            {
                //value = SlacctPasswordUtil.decryption(value.substring(PREFIX_ENCRYPT.length()));
            }
            prop.setProperty(key, value);
        }
        return prop;
    }
    
    /**
     * 属性集中所有键的列表.
     * 
     * @return List&lt;String&gt; 属性集中所有键的列表
     */
    public List<String> propertyNames(Properties prop)
    {
        List<String> nameList = new ArrayList<String>();
        Enumeration<?> names = prop.propertyNames();
        while (names.hasMoreElements())
        {
            nameList.add((String)names.nextElement());
        }
        return nameList;
    }
    
    public void setConfigs(List<String> configs)
    {
        this.configs = configs;
    }
    
}
